package com.zjlp.seproject.controller;

import com.zjlp.seproject.pojo.Comment;
import com.zjlp.seproject.pojo.CommentDetails;
import com.zjlp.seproject.pojo.ID;
import com.zjlp.seproject.service.CommentService;
import com.zjlp.seproject.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     *
     * @param comment  添加的评论内容
     * @param authorization  请求头token
     * @param response  后端相应
     */
    @RequestMapping("/addComment")
    public void addComment(@RequestBody Comment comment,
                           @RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        System.out.println("addComment");
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return;
        }
//        System.out.println(JwtUtil.getUserId(authorization));
        comment.setUid(Integer.parseInt(JwtUtil.getUserId(authorization)));
        commentService.addComment(comment);
    }

    /**
     *
     * @param id  获取的评论用户id
     * @return  返回评论的列表List<CommentDetails>
     */
    @RequestMapping("/getComment")
    public List<CommentDetails> getComment(@RequestBody ID id) {
        System.out.println("getComment");
        return commentService.getCommnet(id.getId());
    }
}
